/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.editpolicy;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;

import ro.pub.cs.se.moondance.command.CompositeStateCreateCommand;
import ro.pub.cs.se.moondance.command.EndStateCreateCommand;
import ro.pub.cs.se.moondance.command.RegionCreateCommand;
import ro.pub.cs.se.moondance.command.SetConstraintCommand;
import ro.pub.cs.se.moondance.command.StartStateCreateCommand;
import ro.pub.cs.se.moondance.command.StateCreateCommand;
import ro.pub.cs.se.moondance.editpart.StateMachineEditPart;
import ro.pub.cs.se.moondance.editpart.UMLEntityEditPart;
import ro.pub.cs.se.moondance.model.UMLCompositeState;
import ro.pub.cs.se.moondance.model.UMLEndState;
import ro.pub.cs.se.moondance.model.UMLEntity;
import ro.pub.cs.se.moondance.model.UMLRegion;
import ro.pub.cs.se.moondance.model.UMLStartState;
import ro.pub.cs.se.moondance.model.UMLState;
import ro.pub.cs.se.moondance.model.UMLStateMachine;

/**
 * Edit policy for Class as a container, handling functionality for dropping new
 * attributes and operations into classes
 */
public class StateMachineLayoutEditPolicy extends XYLayoutEditPolicy {

	/*
	 * (non-Javadoc)
	 * 
	 * @seeConstrainedLayoutEditPolicy#createChangeConstraintCommand(
	 * ChangeBoundsRequest, EditPart, Object)
	 */
	protected Command createChangeConstraintCommand(
			ChangeBoundsRequest request, EditPart child, Object constraint) {
		if (child instanceof UMLEntityEditPart
				&& constraint instanceof Rectangle) {
			// return a command that can move and/or resize a Shape
			return new SetConstraintCommand((UMLEntity) child.getModel(),
					request, (Rectangle) constraint);
		}
		return super.createChangeConstraintCommand(request, child, constraint);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ConstrainedLayoutEditPolicy#createChangeConstraintCommand(EditPart,
	 * Object)
	 */
	protected Command createChangeConstraintCommand(EditPart child,
			Object constraint) {
		// not used in this example
		return null;
	}

	/**
	 * @param request
	 * @return command to handle adding a new attribute/operation
	 */
	protected Command getCreateCommand(CreateRequest request) {
		Object newObject = request.getNewObject();
		if (newObject instanceof UMLState) {
			StateMachineEditPart machinePart = (StateMachineEditPart) getHost();
			UMLStateMachine _stateMachine = machinePart.getStateMachineModel();
			UMLState _state = (UMLState) newObject;
			StateCreateCommand command = new StateCreateCommand(_state,
					_stateMachine, (Rectangle) getConstraintFor(request));
			return command;
		}
		if (newObject instanceof UMLStartState) {
			StateMachineEditPart machinePart = (StateMachineEditPart) getHost();
			UMLStateMachine _stateMachine = machinePart.getStateMachineModel();
			UMLStartState _state = (UMLStartState) newObject;
			StartStateCreateCommand command = new StartStateCreateCommand(
					_state, _stateMachine,
					(Rectangle) getConstraintFor(request));
			return command;
		}
		if (newObject instanceof UMLEndState) {
			StateMachineEditPart machinePart = (StateMachineEditPart) getHost();
			UMLStateMachine _stateMachine = machinePart.getStateMachineModel();
			UMLEndState _state = (UMLEndState) newObject;
			EndStateCreateCommand command = new EndStateCreateCommand(_state,
					_stateMachine, (Rectangle) getConstraintFor(request));
			return command;
		}
		if (newObject instanceof UMLCompositeState) {
			StateMachineEditPart machinePart = (StateMachineEditPart) getHost();
			UMLStateMachine _stateMachine = machinePart.getStateMachineModel();
			UMLCompositeState _state = (UMLCompositeState) newObject;
			CompositeStateCreateCommand command = new CompositeStateCreateCommand(
					_state, _stateMachine,
					(Rectangle) getConstraintFor(request));
			return command;
		}
		if (newObject instanceof UMLRegion) {
			StateMachineEditPart machinePart = (StateMachineEditPart) getHost();
			UMLStateMachine _stateMachine = machinePart.getStateMachineModel();
			UMLRegion _state = (UMLRegion) newObject;
			RegionCreateCommand command = new RegionCreateCommand(_state,
					_stateMachine, (Rectangle) getConstraintFor(request));
			return command;
		}

		return null;

	}
}